import { defineStore } from "pinia";
import { FloorInfo, PositionInfo, StatusInterface } from "@/utils/type";

// 你可以对 `defineStore()` 的返回值进行任意命名，但最好使用 store 的名字，同时以 `use` 开头且以 `Store` 结尾。(比如 `useUserStore`，`useCartStore`，`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
const useMapStore = defineStore("map", {
  state: () => ({
    fmap: null as any,
    center: {
      x: 13530083.260593934,
      y: 3661632.7294,
    }, //中心点，用于判断是否在区域内
    mapOptions: {
      // appName: "shlib", //上图
      // key: "fa03fd7ac0c14ef10138d44364f50e12", //上图
      // mapID: "1504363566993346561", //上图
      //--------------------------------------------------------------------------
      appName: "上海浦东图书馆",
      key: "caed9afdcb3f209bc9b742de0ffc8bf3",
      mapID: "1517149021488873474", //东图
      //--------------------------------------------------------------------------
      // appName: '蜂鸟研发SDK_2_0',
      // key: '57c7f309aca507497d028a9c00207cf8',
      // mapID: '1514920297309614082',
      // level: 4,
      //--------------------------------------------------------------------------
      container: null,
      // mapURL: "https://map-1306178125.cos.ap-shanghai.myqcloud.com/map/",
      // mapURLAbsolute: true,//默认为false,会自动拼接mapID目录层级.
      // themeID: "1506910370410487810", //上图 地图默认主题ID,默认值:'2001'
      themeID: "1772102308430835713", //地图默认主题ID,默认值:'2001'
      backgroundColor: "rgba(227, 231, 236, 1)",
      backgroundAlpha: 0, //地图背景透明度
      mapZoom: 20,
      // rotation: 90,
      zoomRange: [16, 23], //比例尺缩放级别范围(可调范围为1-29),默认值:[16,23]
      enabledPanRange: true,
      // panRangeValue: {
      //   center: { x: 13951602.736502802, y: 5440972.214299999 },
      //   radius: 5,
      // },
    },
    locationParams: {
      // 定位参数
      wssUrl: "wss://indoor.yunweizhi.net:2020", //websocket地址
      locationMapID: "1536945097548865538",
    },
    voiceBaseSrc: "https://pdlib.zenosh.com/api/voice?modelType=1018&text=",
    voiceSrc: "",
    floorInfos: [] as FloorInfo[], //所有楼层信息
    isView3d: true,
    status: {
      map: {
        load: false,
        state: {},
        zoom: 0,
        zoomRange: [],
      },
      bluetooth: false,
      inVenue: false,
    } as StatusInterface,
    websocketObj: null as any,
    mapCenterFlag: false, //更新地图中心点状态
    compassUpdate: false, //罗盘是否更新
    centerFlag: false, //地图中心视角更新状态
    angleFlag: false, //地图旋转角度更新状态
    analyser: null as any, //分析器
    analyserNavigation: null as any, //导航器
    activeFloor: 1, //当前楼层
    deviceMotionFlag: false, //设备定位和运动方向权限申请弹框
    initModel: null as any,
    /* 组件显示的开关 */
    menuFlag: false,
    searchFlag: false,
    listFlag: false,
    detailsFlag: false,
    pathFlag: false,
    navFlag: false,
    routeInputFlag: false,
    routeFlag: false,
    choiceFlag: false,
    aoaFlag: false,
    curPageType: "menu" as
      | "menu"
      | "details"
      | "search"
      | "path"
      | "nav"
      | "list",
    isInAoA: false, //是否在aoa区域
    aoaRecommend: false, //?
    simulateDialog: false, //是否模拟起点申请弹框
    noLocationDialog: false, //不在区域弹框提醒
    routeFailedDialog: false, //路线规划失败
  }),
  getters: {
    getWssUrl: (state) => state.locationParams.wssUrl,
    getLocationMapID: (state) => state.locationParams.locationMapID,
  },
  actions: {
    initAnalyserNavigator() {
      // @ts-ignore
      this.analyser = new fengmap.FMNaviAnalyser(this.mapOptions, () => {
        // @ts-ignore
        this.analyserNavigation = new fengmap.FMNavigation({
          map: this.fmap,
          analyser: this.analyser,
        });
      });
    },
    isContain(data) {
      // @ts-ignore
      return fengmap.FMCalculator.isContain(
        // @ts-ignore ,添加多边形覆盖物
        fengmap.FMCalculator.rectangleBuilder(35, 25, this.center),
        { x: data.x, y: data.y }
      );
    },
    setMapContainer(dom) {
      this.mapOptions.container = dom;
    },
    setLevel(floorId: number) {
      if (floorId != this.activeFloor) {
        this.activeFloor = floorId;
        this.fmap.setLevel({
          level: floorId,
        });
      }
    },
    setZoom(zoom: number) {
      this.status.map.zoom = zoom;
      this.fmap.setZoom({
        zoom: zoom,
      });
    },
    setMapCenter(poi: PositionInfo) {
      if (!this.mapCenterFlag) {
        this.mapCenterFlag = true;
        this.setLevel(poi.floorId);
        this.fmap.setCenter({
          x: poi.x,
          y: poi.y,
          animate: false,
          finish: () => {
            this.mapCenterFlag = false;
          },
        });
      }
    },
    setStatus(value) {
      this.status = { ...this.status, ...value };
    },
    setRotation() {},
  },
});

export default useMapStore();
